# -*- coding:utf-8 -*-
# urllib

import urllib.request
import ssl
import re
import json
import urllib.parse

pat = '<div class="detail-frame">\s*<h2>\s*<a href=".*">(.*?)</a>\s*</h2>'

url = 'https://book.douban.com/latest'

ssl._create_default_https_context = ssl._create_unverified_context
data = urllib.request.urlopen(url).read().decode('utf-8')
resList = re.compile(pat).findall(data)

with open('testFile6', 'w') as f:
    json.dump(resList, f)

with open('testFile6', 'r') as f:
    print(json.load(f))

print(
urllib.request.urlretrieve('http://www.baidu.com/', 'www.baidu.com.html')
)

urllib.request.urlcleanup()

response = urllib.request.urlopen('https://www.baidu.com')
print(response.getcode())
print(response.geturl())


# 百度搜索test
keyword = '雷克萨斯'
url = 'http://www.baidu.com/baidu?tn=monline_3_dg&ie=utf-8&wd=' + urllib.request.quote(keyword)
request = urllib.request.Request(url)
print(type(request))
data = urllib.request.urlopen(request, timeout=2).read()
with open('baidu.html', 'wb') as f:
    f.write(data)

# 模拟POST请求
url = 'http://www.iqianyue.com/mypost'
dataDict = {
    'name' : '张',
    'pass' : 'zhang'
}
data = urllib.parse.urlencode(dataDict).encode('utf-8')
request = urllib.request.Request(url, data)
responseData = urllib.request.urlopen(request).read()
print(responseData.decode('utf-8'))








